package cn.wolfcde.luowowo.website.web.controller;

import cn.wolfcde.luowowo.website.annotation.RedisParameter;
import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.search.service.IDestinationTemplateSearchService;
import cn.wolfcode.luowowo.search.service.IStrategyTemplateSearchService;
import cn.wolfcode.luowowo.search.service.ITravelTemplateSearchService;
import cn.wolfcode.luowowo.search.service.IUserInfoTemplateSearchService;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.template.TravelTemplate;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import sun.security.krb5.internal.crypto.Des;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSessionListener;
import java.util.ArrayList;
import java.util.List;

@Controller
public class DataController {

    @Reference
    private IStrategyCommentService strategyCommentService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IDestinationTemplateSearchService destinationTemplateSearchService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyTemplateSearchService strategyTemplateSearchService;

    @Reference
    private ITravelService travelService;

    @Reference
    private ITravelTemplateSearchService travelTemplateSearchService;

    @Reference
    private IUserInfoService userInfoService;

    @Reference
    private IUserInfoTemplateSearchService userInfoTemplateSearchService;

    @RequestMapping("mongo")
    @ResponseBody
    public Object testMongodb(@RedisParameter UserInfo userInfo) {

        System.out.println(userInfo);

        return userInfo;

    }

    @RequestMapping("datainit")
    @ResponseBody
    public Object initESData() {

        //目的地模板数据初始化
        List<Destination> list = destinationService.list();

        DestinationTemplate destinationTemplate = new DestinationTemplate();

        for (Destination destination : list) {

            BeanUtils.copyProperties(destination, destinationTemplate);

            destinationTemplateSearchService.saveOrUpdate(destinationTemplate);
        }

        //-------------------------------------------------------------------

        //攻略文章数据初始化
        List<StrategyDetail> sd_list = strategyDetailService.list();

        StrategyTemplate t = new StrategyTemplate();

        for (StrategyDetail detail : sd_list) {

            t = new StrategyTemplate();
            t.setId(detail.getId());
            t.setTitle(detail.getTitle());
            t.setSubTitle(detail.getSubTitle());
            t.setDestId(detail.getDest().getId());
            t.setDestName(detail.getDest().getName());
            t.setThemeId(detail.getTheme().getId());
            t.setThemeName(detail.getTheme().getName());
            t.setSummary(detail.getSummary());
            t.setCreateTime(detail.getCreateTime());
            t.setViewnum(detail.getViewnum());
            t.setFavornum(detail.getFavornum());
            t.setReplynum(detail.getReplynum());
            t.setThumbsupnum(detail.getThumbsupnum());

            t.setCoverUrl(detail.getCoverUrl());

            //设置省份信息
            Destination province = this.getProvinceByDestID(detail.getDest().getId());
            if(province != null){

                t.setProvinceName(province.getName());
                t.setProvinceId(province.getId());

            }

            //设置国家信息
            Destination country = this.getCountryByDestID(detail.getDest().getId());
            if(country != null){

                t.setCountryName(country.getName());
                t.setCountryId(country.getId());

            }

            //初始化数据
            strategyTemplateSearchService.saveOrUpdate(t);
        }

        //-------------------------------------------------------------------

        //游记的模板信息初始化
        List<Travel> tr_list = travelService.list();

        TravelTemplate tt = new TravelTemplate();

        for (Travel travel : tr_list) {

            tt.setId(travel.getId());
            tt.setAuthorId(travel.getAuthor().getId());
            tt.setAuthorName(travel.getAuthor().getNickname());
            tt.setCoverUrl(travel.getCoverUrl());
            tt.setCreateTime(travel.getCreateTime());
            tt.setDestId(travel.getDest().getId());
            tt.setDestName(travel.getDest().getName());
            tt.setReplynum(travel.getReplynum());
            tt.setSummary(travel.getSummary());
            tt.setTitle(travel.getTitle());
            tt.setViewnum(travel.getViewnum());

            travelTemplateSearchService.saveOrUpdate(tt);

        }

        //-------------------------------------------------------------------

        //用户初始化--------------------------
        List<UserInfo> us = userInfoService.list();
        for (UserInfo u : us) {
            UserInfoTemplate ttt = new UserInfoTemplate();
            ttt.setId(u.getId());
            ttt.setDestName(u.getCity());
            ttt.setFansnum(11);
            ttt.setHeadUrl(u.getHeadImgUrl());
            ttt.setInfo(u.getInfo());
            ttt.setLevel(u.getLevel());
            ttt.setNickname(u.getNickname());
            ttt.setReplynum(10);
            ttt.setTravelnum(2);
            userInfoTemplateSearchService.saveOrUpdate(ttt);
        }


        return "ok";

    }





    /**
     * 根据destId获取省份的目的地对象
     *
     * @param destId
     */
    private Destination getProvinceByDestID(Long destId) {

        if (destId != null) {
            List<Destination> toasts = destinationService.getToasts(destId);

            if (toasts.size() >= 2 && toasts.get(0).getId() == 1) {
                return toasts.get(1);
            }
        }
        return null;

    }

    /**
     * 根据destId获取国家的目的地对象
     *
     * @param destId
     */
    private Destination getCountryByDestID(Long destId) {
        if (destId != null) {
            List<Destination> toasts = destinationService.getToasts(destId);

            if (toasts.get(0).getDeep() == 1) {
                return toasts.get(0);
            }
        }
        return null;
    }

//    @RequestMapping("flush")
//    @ResponseBody
//    public Object st1(HttpServletRequest request, HttpServletResponse response){
//
//        String ip = request.getRemoteAddr(); //得到客户端IP地址
//
//        String requestURI = request.getRequestURI();//得到客户请求的uri
//
//        StringBuilder stringBuilder = new StringBuilder();
//
//        stringBuilder.append(ip).append(":").append(requestURI);
//
//        return stringBuilder.toString();
//    }
//

}
